package com.heima.admin.job;

import com.heima.admin.service.WemediaNewsAutoScanService;
import com.heima.feigns.wemedia.WemediaFeign;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@Component
@Slf4j
public class WeMediaNewsAutoScanJob {


    @Autowired
    WemediaFeign wemediaFeign;

    @Autowired
    WemediaNewsAutoScanService wemediaNewsAutoScanService;

    /**
     * 定时发布文章的方法
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob("wemediaAutoScanJob")
    public ReturnT<String> autoScanJob(String param) throws Exception {
        log.info("*****  WeMediaNewsAutoScanJob  autoScanJob is begin *****");

        try {
            //1 查所有待发布的文章id列表
            List<Integer> wmNewsIds = wemediaFeign.findRelease();
            log.info("wmNewsIds:{}",wmNewsIds);
            //2 调用发布文章方法
            if (wmNewsIds != null && wmNewsIds.size() > 0) {
                for (Integer wmNewsId : wmNewsIds) {
                    wemediaNewsAutoScanService.autoScanByMediaNewsId(wmNewsId);
                }
            }

            log.info("*****  WeMediaNewsAutoScanJob  autoScanJob is success wmNewsIds: {}",wmNewsIds);
            return ReturnT.SUCCESS;
        } catch (Exception e) {
//            e.printStackTrace();
            log.info("*****  WeMediaNewsAutoScanJob  autoScanJob is error *****, {}", e);
        }



        return ReturnT.FAIL;
    }

}
